package com.hypo.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hypo.business.domain.dto.PageResultDto;
import com.hypo.business.domain.dto.WarehouseAreaLocalOpDto;
import com.hypo.business.domain.dto.WarehouseAreaLocalPageDto;
import com.hypo.business.domain.po.WarehouseAreaLocal;
import com.hypo.business.mapper.WarehouseAreaLocalMapper;
import com.hypo.business.mapper.WarehouseAreaMapper;
import com.hypo.business.mapper.WarehouseMapper;
import com.hypo.business.service.WarehouseAreaLocalService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author hypo
 * @version 1.0
 * @since 2025/8/13
 */
@Service
@RequiredArgsConstructor
public class WarehouseAreaLocalServiceImpl extends ServiceImpl<WarehouseAreaLocalMapper, WarehouseAreaLocal> implements WarehouseAreaLocalService {
    private final WarehouseAreaLocalMapper warehouseAreaLocalMapper;

    private final WarehouseAreaMapper warehouseAreaMapper;

    private final WarehouseMapper warehouseMapper;

    /**
     * 修改库位信息
     */
    @Override
    public void warehouseAreaLocalUpdate(WarehouseAreaLocalOpDto dto) {
        WarehouseAreaLocal warehouseAreaLocal = new WarehouseAreaLocal();
        BeanUtil.copyProperties(dto, warehouseAreaLocal);
        warehouseAreaLocal.setUpdateTime(LocalDateTime.now());
        warehouseAreaLocalMapper.updateById(warehouseAreaLocal);
    }

    /**
     * 新增库位信息
     */
    @Override
    public void warehouseAreaLocalAdd(WarehouseAreaLocalOpDto dto) {
        WarehouseAreaLocal warehouseAreaLocal = new WarehouseAreaLocal();
        BeanUtil.copyProperties(dto, warehouseAreaLocal);
        warehouseAreaLocal.setCreateTime(LocalDateTime.now());
        warehouseAreaLocal.setUpdateTime(LocalDateTime.now());
        warehouseAreaLocalMapper.insert(warehouseAreaLocal);
    }

    /**
     * 仓库货位分页
     */
    @Override
    public PageResultDto<WarehouseAreaLocal> warehouseAreaLocalPage(WarehouseAreaLocalPageDto dto) {
        Page<WarehouseAreaLocal> page = new Page<>(dto.getPage(), dto.getPageSize());
        Page<WarehouseAreaLocal> pageInfo = warehouseAreaLocalMapper.selectPage(page, Wrappers.<WarehouseAreaLocal>lambdaQuery()
                .eq(ObjectUtil.isNotEmpty(dto.getWarehouseId()), WarehouseAreaLocal::getWarehouseId, dto.getWarehouseId())
                .eq(ObjectUtil.isNotEmpty(dto.getWarehouseAreaId()), WarehouseAreaLocal::getWarehouseAreaId, dto.getWarehouseAreaId()));
        pageInfo.getRecords().forEach(warehouseAreaLocal -> {
            warehouseAreaLocal.setWarehouseName(warehouseMapper.selectById(warehouseAreaLocal.getWarehouseId()).getName());
            warehouseAreaLocal.setWarehouseAreaName(warehouseAreaMapper.selectById(warehouseAreaLocal.getWarehouseAreaId()).getName());
        });
        return new PageResultDto<>(pageInfo.getTotal(), pageInfo.getRecords());
    }
}
